package com.hmkj.core.service.quote;

import com.hmkj.core.constant.CacheID;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.ModelIndexRealPrice;
import com.hmkj.core.eolinker.models.StockListModel;
import com.hmkj.core.service.cache.RedisService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jack
 * @date 2017/8/17
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class QuoteService {

    @Resource
    RedisService redisService;

    /**
     * 2A01 深证指数  395004 创业板 1A0001上证
     *
     * @return
     */
    public List<ModelIndexRealPrice> findRealTimeListJson() throws Exception {
        List<ModelIndexRealPrice> list = redisService.getList(CacheID.STOCK_INDEX, ModelIndexRealPrice.class);
        if (CollectionUtils.isEmpty(list)) {
            ModelIndexRealPrice shangzheng = QuotationEndpoint.index("1A0001");
            ModelIndexRealPrice shenzheng = QuotationEndpoint.index("2A01");
            ModelIndexRealPrice chuanye = QuotationEndpoint.index("399006");
            if (shangzheng != null) {
               list.add(shangzheng);
            }
            if (shenzheng != null) {
                list.add(shenzheng);
            }
            if (chuanye != null) {
                list.add(chuanye);
            }
        }
        return list;

    }

    /**
     * 获取综合行情数据、条数(0,所有)
     *
     * @return
     */
    public List<StockListModel> findStockList(int size) throws Exception {
        List<StockListModel> resultList = new ArrayList<>();
        //上证A股
        List<StockListModel> upList = redisService.getList(CacheID.UP_STOCK_PREFIX, StockListModel.class);
        if (null != upList && upList.size() > 0) {
            for (StockListModel upStock : upList) {
                resultList.add(upStock);
            }
        }
        //深证A股
        List<StockListModel> shenList = redisService.getList(CacheID.SHEN_STOCK_PREFIX, StockListModel.class);
        if (null != upList && upList.size() > 0) {
            for (StockListModel shenStock : shenList) {
                resultList.add(shenStock);
            }
        }
        //上证指数
        List<StockListModel> upIndexList = redisService.getList(CacheID.UPINDEX_STOCK_PREFIX, StockListModel.class);
        if (null != upList && upList.size() > 0) {
            for (StockListModel upIndexStock : upIndexList) {
                resultList.add(upIndexStock);
            }
        }
        //深证指数
        List<StockListModel> shenIndexList = redisService.getList(CacheID.SHENINDEX_STOCK_PREFIX, StockListModel.class);
        if (null != upList && upList.size() > 0) {
            for (StockListModel shenIndexStock : shenIndexList) {
                resultList.add(shenIndexStock);
            }
        }
        //中小板指数
        List<StockListModel> msList = redisService.getList(CacheID.MS_STOCK_PREFIX, StockListModel.class);
        if (null != upList && upList.size() > 0) {
            for (StockListModel msStock : msList) {
                resultList.add(msStock);
            }
        }
        //创业板指数
        List<StockListModel> starUpList = redisService.getList(CacheID.STARTUPS_STOCK_PREFIX, StockListModel.class);
        if (null != upList && upList.size() > 0) {
            for (StockListModel starUpStock : starUpList) {
                resultList.add(starUpStock);
            }
        }
        return resultList;
    }

}
